package nongchang.kuaijiecaifu.com.ncapp_android.ui.adapter.basketadapter;

import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.support.v7.widget.RecyclerView;
import android.text.Editable;
import android.text.TextWatcher;
import android.view.View;
import android.view.ViewGroup;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.bumptech.glide.Glide;

import java.util.ArrayList;

import butterknife.BindView;
import butterknife.ButterKnife;
import ezy.lite.util.ContextUtil;
import ezy.lite.util.Param;
import ezy.lite.util.UI;
import ezy.widget.view.AutofitImageView;
import nongchang.kuaijiecaifu.com.ncapp_android.R;
import nongchang.kuaijiecaifu.com.ncapp_android.common.adapter.ItemAdapter;
import nongchang.kuaijiecaifu.com.ncapp_android.retrofit.data.HomeCartManager;
import nongchang.kuaijiecaifu.com.ncapp_android.retrofit.good.AllCart;
import nongchang.kuaijiecaifu.com.ncapp_android.retrofit.good.BasketCart;
import nongchang.kuaijiecaifu.com.ncapp_android.retrofit.good.Cart;
import nongchang.kuaijiecaifu.com.ncapp_android.retrofit.good.HomeItemCart;
import nongchang.kuaijiecaifu.com.ncapp_android.ui.BasketDetailActivity;
import nongchang.kuaijiecaifu.com.ncapp_android.ui.BasketMultipleDetailActivity;
import nongchang.kuaijiecaifu.com.ncapp_android.ui.GameRuleActivity;

/**
 * Created by Administrator on 2016/5/4 0004.
 */
public class BasketCartAdapter extends ItemAdapter<AllCart, BasketCartAdapter.BasketCartViewHolder> {
    private Context context;

    public BasketCartAdapter(Context context) {
        super(context);
        this.context = context;
    }

    private View.OnClickListener onAddNum;  //加商品数量接口
    private View.OnClickListener onSubNum;  //减商品数量接口
    private View.OnFocusChangeListener onTotalChange;
    private TextView.OnEditorActionListener onTotalTouch;

    public void setOnAddNum(View.OnClickListener onAddNum) {
        this.onAddNum = onAddNum;
    }

    public void setOnTotalNum(View.OnFocusChangeListener onTotalNum, TextView.OnEditorActionListener onTotalTouch) {
        this.onTotalChange = onTotalNum;
        this.onTotalTouch = onTotalTouch;
    }

    public void setOnSubNum(View.OnClickListener onSubNum) {
        this.onSubNum = onSubNum;
    }


    @Override
    public BasketCartViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        return new BasketCartViewHolder(inflate(R.layout.item_basket_cart, parent));
    }

    @Override
    public void onBindViewHolder(BasketCartViewHolder holder, int position) {
        AllCart item = getItem(position);
        //========================================================================        这是单品的==============================================================================================================================================
        if (item.type == 2) {
            holder.relativeLayout.setVisibility(View.VISIBLE);
            holder.hotRelativeLayout.setVisibility(View.GONE);
            Glide.with(context).load(item.image).into(holder.imgCart);
            holder.txtTitle.setText(item.title);
//            UI.setTvHtml(holder.txtOncePrice, R.string.basket_one_piece, item.one_money, item.weight, item.getOnePiece());

            UI.setTvHtml(holder.txtTotal, R.string.basket_total, item.number, item.getTotalWeight());
            holder.editText.setText(item.number + "");
            holder.imgCart.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    Intent intent = new Intent(v.getContext(), BasketDetailActivity.class);
                    intent.putExtra("hotType", item.type);
                    intent.putExtra("money", item.one_money);
                    v.getContext().startActivity(intent);
                }
            });
//            增加数量
            holder.btnAdd.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    int num = UI.toInteger(holder.editText);
                    num++;
                    holder.editText.setText(num + "");
                    setSimpleUpdate(item, num);
                }
            });
//            减少数量
            holder.btnSub.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    int num = UI.toInteger(holder.editText);
                    if (num <= 0) {
                        return;
                    }
                    num--;
                    if (num == 0) {
                        HomeCartManager.local.remove(item.unitId);
                        holder.editText.setText("0");
                    } else {
                        holder.editText.setText(num + "");
                        setSimpleUpdate(item, num);
                    }
                }
            });
            holder.editText.setOnFocusChangeListener(new View.OnFocusChangeListener() {
                @Override
                public void onFocusChange(View v, boolean hasFocus) {
                    if (hasFocus) {
                        holder.editText.addTextChangedListener(new TextWatcher() {
                            @Override
                            public void beforeTextChanged(CharSequence s, int start, int count, int after) {

                            }

                            @Override
                            public void onTextChanged(CharSequence s, int start, int before, int count) {

                            }

                            @Override
                            public void afterTextChanged(Editable s) {
                                if (s.toString().equals("")) {
                                    holder.editText.setText("0");
                                    HomeCartManager.local.remove(item.unitId);
                                }
                            }
                        });
                    } else {
                        setSimpleUpdate(item, UI.toInteger(holder.editText));
                    }
                }
            });
        }
        //========================================================================        这是单品的===========================================================================================================================================

//        =================================================================这是套餐的==============================================================================================================================================
        else {
            holder.relativeLayout.setVisibility(View.GONE);
            holder.hotRelativeLayout.setVisibility(View.VISIBLE);
            holder.txtHotTitle.setText(item.title);
            Glide.with(context).load(item.image).into(holder.imgIcon);
            holder.txt_hot_total.setText("2斤/5个");
            holder.txt_hot_once_price.setText("￥" + item.one_money);
            holder.btn_hot_check.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    ContextUtil.startActivity(v.getContext(), BasketMultipleDetailActivity.class, Param.bundle(item));
                }
            });
            holder.btn_hot_sub.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    int num = UI.toInteger(holder.edit_hot_total);
                    if (num <= 0) {
                        return;
                    }
                    num--;
                    if (num == 0) {
                        HomeCartManager.local.remove(item.unitId);
                        holder.edit_hot_total.setText("0");
                    } else {
                        holder.edit_hot_total.setText(num + "");
                        setUpdate(item, num);
                    }
                }
            });
            holder.btn_hot_add.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    int num = UI.toInteger(holder.edit_hot_total);
                    num++;
                    holder.edit_hot_total.setText(num + "");
                    setUpdate(item, num);
                }
            });
        }
        holder.edit_hot_total.setOnFocusChangeListener(new View.OnFocusChangeListener() {
            @Override
            public void onFocusChange(View v, boolean hasFocus) {
                if (hasFocus) {
                    holder.edit_hot_total.addTextChangedListener(new TextWatcher() {
                        @Override
                        public void beforeTextChanged(CharSequence s, int start, int count, int after) {

                        }

                        @Override
                        public void onTextChanged(CharSequence s, int start, int before, int count) {

                        }

                        @Override
                        public void afterTextChanged(Editable s) {
                            if (s.toString().equals("")) {
                                holder.edit_hot_total.setText("0");
                                HomeCartManager.local.remove(item.unitId);
                            }
                        }
                    });
                } else {
                    setUpdate(item, UI.toInteger(holder.edit_hot_total));
                }
            }
        });
//        =================================================================这是套餐的==============================================================================================================================================
    }

    private AllCart allCart;

    private void setUpdate(AllCart cart, int num) {
        allCart = new AllCart();
        allCart.itemList = new ArrayList<>(cart.itemList);
        int sum = 0;
        for (int i = 0, size = cart.itemList.size(); i < size; i++) {
            allCart.itemList.get(i).num = num;
            allCart.itemList.get(i).total_money = cart.itemList.get(i).one_price * num;
            sum += allCart.itemList.get(i).total_money;
        }
        allCart.money = sum;
        allCart.type = 1;
        allCart.number = 1;
        allCart.unitId = cart.unitId;
        allCart.isSelect = true;
        allCart.title = cart.title;
        allCart.one_money = cart.one_money;
        HomeCartManager.local.add(allCart);
    }

    private void setSimpleUpdate(AllCart cart, int num) {
        allCart = new AllCart();
        allCart.number = num;
        allCart.money = cart.one_money * num;
        allCart.type = 2;
        allCart.unitId = cart.unitId;
        allCart.isSelect = true;
        allCart.image = cart.image;
        allCart.title = cart.title;
        allCart.one_money = cart.one_money;
        HomeCartManager.local.add(allCart);
    }


    static class BasketCartViewHolder extends RecyclerView.ViewHolder {
        @BindView(R.id.img_cart)
        ImageView imgCart;
        @BindView(R.id.img_icon)
        ImageView imgIcon;
        @BindView(R.id.txt_title)
        TextView txtTitle;

        @BindView(R.id.txt_once_price)
        TextView txtOncePrice;

        @BindView(R.id.txt_total)
        TextView txtTotal;

        @BindView(R.id.btn_sub)
        ImageView btnSub;

        @BindView(R.id.btn_add)
        ImageView btnAdd;

        @BindView(R.id.edit_total)
        EditText editText;

        @BindView(R.id.btn_hot_sub)
        ImageView btn_hot_sub;

        @BindView(R.id.btn_hot_add)
        ImageView btn_hot_add;

        @BindView(R.id.edit_hot_total)
        EditText edit_hot_total;
        @BindView(R.id.rl_layout)
        RelativeLayout relativeLayout;
        @BindView(R.id.rl_hot_layout)
        RelativeLayout hotRelativeLayout;

        @BindView(R.id.txt_hot_title)
        TextView txtHotTitle;

        @BindView(R.id.txt_hot_once_price)
        TextView txt_hot_once_price;
        @BindView(R.id.txt_hot_total)
        TextView txt_hot_total;
        @BindView(R.id.btn_hot_check)
        TextView btn_hot_check;

        public BasketCartViewHolder(View itemView) {
            super(itemView);
            ButterKnife.bind(this, itemView);
        }


    }
}
